<?php
if (!defined('ipos')) die("Please contact Y!m: ichphien_pro or Email: ichphien_pro@yahoo.com.");

function get_type($filename) {
	$start = explode(".",$filename);
	$count = count($start)-1;
	return $start[$count];
}

// thu nho anh
function imagesthumb($target, $newcopy, $ext,$w=250,$h=500) {
    list($w_orig, $h_orig) = getimagesize($target);
    $scale_ratio = $w_orig / $h_orig;
    if (($w / $h) > $scale_ratio) {
           $w = $h * $scale_ratio;
    } else {
           $h = $w / $scale_ratio;
    }
    $img = "";
    $ext = strtolower($ext);
    if ($ext == "gif"){ 
    $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
    $img = imagecreatefrompng($target);
    } else { 
    $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    // imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
    imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
    if ($ext == "gif"){ 
        imagegif($tci, $newcopy);
    } else if($ext =="png"){ 
        imagepng($tci, $newcopy);
    } else { 
        imagejpeg($tci, $newcopy, 100);
    }
}
// cat anh
function cropimages($target, $newcopy, $ext,$w=250,$h=160) {
    list($w_orig, $h_orig) = getimagesize($target);
    $src_x = ($w_orig / 2) - ($w / 2);
    $src_y = ($h_orig / 2) - ($h / 2);
    $ext = strtolower($ext);
    $img = "";
    if ($ext == "gif"){ 
    $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
    $img = imagecreatefrompng($target);
    } else { 
    $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w, $h);
    if ($ext == "gif"){ 
        imagegif($tci, $newcopy);
    } else if($ext =="png"){ 
        imagepng($tci, $newcopy);
    } else { 
        imagejpeg($tci, $newcopy, 100);
    }
}

// upload ảnh
function ipupload($name,$foder,$fileoner,$array_ext = array('jpg','jpeg','png','gif','JPG','JPEG','PNG','GIF','srt','SRT')) {
	$ichphienpro	=	$foder;
	$oldumask = umask(0);
		@mkdir(UPLOAD_DIR, 0777);
		@mkdir(UPLOAD_DIR."/".date("Y"), 0777);
		@mkdir(UPLOAD_DIR."/".date("Y")."/".date("m"), 0777);
		@mkdir(UPLOAD_DIR."/".date("Y")."/".date("m")."/".date("d"), 0777);
		@mkdir(UPLOAD_DIR."/".date("Y")."/".date("m")."/".date("d")."/".$foder, 0777);
	umask($oldumask);
	$foder	=	date("Y")."/".date("m")."/".date("d")."/".$foder;
	if($_FILES["$name"]['name']!="") {
		$fileupload			=	NOW.$_FILES["$name"]['name'];
		$file_EXT			=	get_type($fileupload);
		if(!in_array($file_EXT, $array_ext)) {
			return	1;
		}else {
			$fileupload			=	$fileoner.'.'.$file_EXT;
			$uploaddir			=	UPLOAD_DIR."/".$foder."/".$fileupload;
			// tiến hành upload ảnh
			if(@move_uploaded_file ($_FILES["$name"]['tmp_name'],$uploaddir)) {
				if($ichphienpro == "film") {
					imagesthumb($uploaddir,$uploaddir,$file_EXT,218);
					$images	=	UPLOAD_FOLDER."/".$foder."/".$fileupload;
				}
				elseif($ichphienpro == "info") {
					imagesthumb($uploaddir,$uploaddir,$file_EXT,950);
					$images	=	UPLOAD_FOLDER."/".$foder."/".$fileupload;
				}
				else {
					$images	=	UPLOAD_FOLDER."/".$foder."/".$fileupload;
				}
			}
			return	$images;
		}
	}
}
?>